package com.pen.interfece.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pen.interfece.mapper.DormitoryMapper;
import com.pen.interfece.mapper.StudentMapper;
import com.pen.interfece.pojo.PageResult;
import com.pen.interfece.pojo.dto.StudentDTO;
import com.pen.interfece.pojo.dto.StudentPageQueryDTO;
import com.pen.interfece.pojo.entity.Admin;
import com.pen.interfece.pojo.entity.Disciplinary;
import com.pen.interfece.pojo.entity.Dormitory;
import com.pen.interfece.pojo.entity.Student;
import com.pen.interfece.service.StudentSerivice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentSeriviceImpl implements StudentSerivice {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Override
    public void save(StudentDTO studentdto, int id) {
        Student student=new Student();
        BeanUtils.copyProperties(studentdto,student);
        student.setDormitoryId(id);
        student.setPoint(100);
        Dormitory dormitory=dormitoryMapper.selectById(id);
        String floor= dormitory.getFloor();
        int dormitoryNumber=dormitory.getDormitoryNumber();
        student.setFloor(floor);
        student.setDormitoryNumber(dormitoryNumber);
        studentMapper.insert(student);
    }

    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            studentMapper.deleteById(id);
        }
    }

    @Override
    public void update(StudentDTO studentDTO) {
        studentMapper.update(studentDTO);
    }

    @Override
    public void addDisciplinary(Disciplinary disciplinary, int id) {
        disciplinary.setTime(LocalDateTime.now());
        disciplinary.setStudentId(id);
        //向违纪表中增加一条违纪
        studentMapper.addDisciplinary(disciplinary);
        int point=disciplinary.getNumber();
        Student student=studentMapper.getStudentById(id);
        int point2=student.getPoint()-point;
        StudentDTO studentDTO=new StudentDTO();
        studentDTO.setId(id);
        studentDTO.setPoint(point2);
        studentMapper.update(studentDTO);
    }

    @Override
    public PageResult pageQuery(StudentPageQueryDTO studentPageQueryDTO) {
        //开始分页查询，插件执行
        PageHelper.startPage(studentPageQueryDTO.getPage(),studentPageQueryDTO.getPageSize());
        Page<Student> page=studentMapper.pageQuery(studentPageQueryDTO);

        long total = page.getTotal();
        List<Student> records = page.getResult();

        return new PageResult(total,records);
    }
}
